const app = getApp();
var router = require("../../router.js");
var animate = require('../../utils/animate.js');
var WxParse = require('../../wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show_confirm_box: false,
    fadeIn: '',
    safe: false,
    show_tj: false,
    tj_info: { //推荐活动信息     
    },//推荐信息
    animationData: '',//推荐弹窗过度动画
    animationData2: '',//遮罩层的过度动画
    pay_info: '',
    pay_price: 0
  },
  buy() {
    var style = this.data.list.style;
    console.log(style);
    if (style == 2){
      wx.navigateTo({
        url: 'people_list/people_list',
      })
    }else{    
      var that = this;
      app.request({
        url: router.course.add,
        method: "POST",
        data: {
          id: that.data.list.id,
          user_id: wx.getStorageSync('userInfo').id
        },
        success: function (data) {
          console.log(data);          
          if (data.status == true) {
            wx.showToast({
              title: '操作成功',             
            });
            that.setData({
              pay_price: data.price
            });      
            if(data.coupon.id){            
              that.setData({               
                tj_info: { //推荐活动信息
                  id: data.coupon.id,//标题
                  max: data.coupon.max,//标题
                  min: data.coupon.min,
                  title: data.coupon.title,
                },
              });              
            }
            if (data.price > 0 && data.info) {
              that.setData({
                pay_info: data.info,
                show_confirm_box: true
              });
              let animation = wx.createAnimation({
                duration: '300',
                timingFunction: 'ease',
              })
              animation.translate(0, -50).opacity(1).step()
              that.setData({
                animationData: animation.export()
              })
              let animation2 = wx.createAnimation({
                duration: '300',
                timingFunction: 'ease',
              })
              animation2.opacity(1).step()
              that.setData({
                fadeIn: animation2.export()
              })
            }else{
              that.showInfo();
            } 
          } else {
            wx.showToast({
              title: '操作失败',
              icon: 'none',
              mask: true,
              duration: 2000
            });
          }
        }
      })
    }  
  },

  pay: function (data) {
    var payInfo = this.data.pay_info;
    if (!payInfo) {
      wx.showToast({
        title: '支付失败',
        icon: 'none'
      });
      return false;
    }
    var that = this;
    wx.requestPayment({
      'timeStamp': payInfo.timeStamp,
      'nonceStr': payInfo.nonceStr,
      'package': payInfo.package,
      'signType': payInfo.signType,
      'paySign': payInfo.paySign,
      'success': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:ok') {
          wx.showToast({
            title: '支付成功',
            icon: 'none'
          })
          that.setData({
            show_confirm_box: false
          });
          that.showInfo();
        }
      },
      'fail': function (res) {
        console.log(res.errMsg)
        if (res.errMsg == 'requestPayment:fail cancel') {
          wx.showToast({
            title: '取消支付',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      },
      'complete': function (res) {
      }
    })
  },

  showInfo: function () {
    if (this.data.tj_info.id) {
      this.setData({
        show_tj: true,
      });
      animate.fade_up(this);
    } else {
      app.navTo('../mycourse/mycourse');
    }
  },

  clsoe_confirm_box() {
    let animation = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation.translate(0, 0).opacity(0).step()
    this.setData({
      animationData: animation.export()
    })
    let animation2 = wx.createAnimation({
      duration: '300',
      timingFunction: 'ease',
    })
    animation2.opacity(0).step()
    this.setData({
      fadeIn: animation2.export()
    })
    setTimeout(() => {
      this.setData({
        show_confirm_box: false
      });
    }, 300)
    this.showInfo();
  },

  get_coupon: function(){    
    var that = this;
    app.request({
      url: router.course.coupon,
      method: "POST",
      data: {
        id: that.data.tj_info.id,
        user_id: wx.getStorageSync('userInfo').id
      },
      success: function (data) {
        console.log(data);      
        if (data.status == true) {
          wx.showToast({
            title: '操作成功',
            icon: 'none',
            mask: true,
            duration: 2000
          });        
          app.navTo('../mycard/mycard');        
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'none',
            mask: true,
            duration: 2000
          });
        }
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      expand_id: options.id
    });   
  },

  //分享
  onShareAppMessage: function (res) {   
    if (res.from === 'button') {     
      var that = this;
      // 来自页面内转发按钮    
      var id = wx.getStorageSync("userInfo")['id'];  
      var title =  that.data.list.title;
      return {
        title: title,
        path: '/pages/index/index?scene=' + id,
        success: function (res) {             
          wx.showToast({
            title: '分享成功',
            icon: 'success',
            duration: 2000
          });                     
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
    wx.showShareMenu({
      withShareTicket: true
    })
  },
  getInfo: function () {    
    var that = this;
    app.request({
      url: router.course.info,
      method: "POST",
      data: {
        id: that.data.expand_id,
        user_id: wx.getStorageSync("userInfo")['id'],
      },
      success: function (data) {
        console.log(data);
        //return false;
        if (data.status == true) {
          that.setData({
            list: data.list,            
          });
          WxParse.wxParse('article', 'html', data.list.info, that, 5);
          if (data.list.style == 2){            
            that.setData({
              safe: true
            });
            wx.setStorage({
              key: "expand_list",
              data: data.list,
            });    
          }              
        } else {
          wx.showToast({
            title: '获取信息失败',
            icon: 'none',
            mask: true,
            duration: 2000
          });
        }
      }
    })    
  }
})